package DCSource.PluginsV2;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;

import javax.swing.JFrame;

public class processList {
	private static ArrayList<PluginV2> list = new ArrayList<PluginV2>();
	private static ArrayList<JFrame> list2 = new ArrayList();
	private static ArrayList<Thread> list3 = new ArrayList();
	private static boolean Blocked = false;
	public static void add(PluginV2 M){
		if (Blocked==false){
		list.add(M);}
	}
	public static void add(PluginV2[] M){
		if (Blocked==false){
		for (int I=0;I<M.length;I++){
			add(M[I]);
		}
		}
	}
	public static void add(ArrayList<PluginV2> M){
		if (Blocked==false){
		for (int I=0;I<M.size();I++){
			add(M.get(I));
		}
		}
	}
	public static void add(Thread t){
		if (Blocked==false){
		list3.add(t);
		}
	}
	public static void block(){
		Blocked=true;
		clear();
	}
	public static void rev(JFrame M){
		for (int I = 0;I<list2.size();I++){
			if (M.equals(list2.get(I))){
				try{
					list2.get(I).dispose();
				}catch(Exception e){}
				list2.remove(I);
			}
		}
	}
	public static void rev(Thread M){
		for (int I = 0;I<list3.size();I++){
			if (M.equals(list3.get(I))){
				M.stop();
				list3.remove(I);
			}
		}
	}
	public static void revPlugin(int DEX){
		if (DEX>=0 & DEX<list.size()){
			list.get(DEX).onExit();
			list.remove(DEX);
		}
	}
	public static void clear(){
		closeProcess();
		list.clear();
		list2.clear();
		list3.clear();
	}
	public static void rev(PluginV2 M){
		for (int I = 0;I<list.size();I++){
			if (M.equals(list.get(I))){
				list.get(I).onExit();
				list.remove(I);
			}
		}
	}
	public static void startProcess(){
		for (int I = 0;I<list.size();I++){
			list.get(I).onStart();
		}
	}
	public static void refreshProcess(){
		for (int I = 0;I<list.size();I++){
			list.get(I).onRefresh();
		}
	}
	public static void closeProcess(){
		exitRefresh();
		for (int I = 0;I<list.size();I++){
			list.get(I).onExit();
		}
		for (int I = 0;I<list2.size();I++){
			try{
				list2.get(I).dispose();
			}catch(Exception e){}
		}
		for (int I = 0;I<list3.size();I++){
			try{
				list3.get(I).stop();
			}catch(Exception e){}
		}
		list2.clear();
	}
	public static void add(JFrame frame){
		if (Blocked==false){
		list2.add(frame);}
	}
	public static void miniProcess(){
		for (int I = 0;I<list.size();I++){
			list.get(I).onMinimide();
		}
	}
	public static void showProcess(){
		for (int I = 0;I<list.size();I++){
			list.get(I).onShow();
		}
	}
	public static void printCommand(String cmd){
		for (int I = 0;I<list.size();I++){
			list.get(I).onCommand(cmd);
		}
	}
	public static void printCommandTo(String title,String cmd){
		for (int I = 0;I<list.size();I++){
			if (list.get(I).getTitle().equals(title)){
				list.get(I).onCommand(cmd);
			}
		}
	}
	static Thread refresh = new refresh();
	public static void initRefresh(){
		if (refresh.isAlive()==false){
			try{
			refresh.start();}catch(Exception err){}
		}
	}
	public static void exitRefresh(){
		if (refresh.isAlive()==true){
			refresh.stop();
			refresh = new refresh();
		}
	}
	public static ArrayList<PluginV2> getPlugins(){
		return list;
	}
	public static WindowListener getWindowListener(){
		return new WindowListener(){
			public void windowActivated(WindowEvent arg0) {
				showProcess();
			}
			public void windowClosed(WindowEvent arg0) {
				
			}
			public void windowClosing(WindowEvent arg0) {
				closeProcess();
			}
			public void windowDeactivated(WindowEvent arg0) {
				miniProcess();
			}
			public void windowDeiconified(WindowEvent arg0) {
				
			}
			public void windowIconified(WindowEvent arg0) {
				
			}
			public void windowOpened(WindowEvent arg0) {
			
			}};
	}
}
class refresh extends Thread{
	public void run(){
		while(true){
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			}
			processList.refreshProcess();
		}}
}